package com.e3roid.examples;

import javax.microedition.khronos.opengles.GL10;

import android.view.MotionEvent;
import android.widget.Toast;

import com.e3roid.E3Activity;
import com.e3roid.E3Engine;
import com.e3roid.E3Scene;
import com.e3roid.drawable.Background;
import com.e3roid.drawable.Sprite;
import com.e3roid.drawable.texture.Texture;
import com.e3roid.drawable.texture.AssetTexture;
import com.e3roid.drawable.texture.TiledTexture;
import com.e3roid.util.Debug;

public class SpriteRemoveExample extends E3Activity {

	private final static int WIDTH  = 320;
	private final static int HEIGHT = 480;
	
	private Texture robotTexture;
	private Sprite robot;

	@Override
	public E3Engine onLoadEngine() {
		E3Engine engine = new E3Engine(this, WIDTH, HEIGHT);
		engine.requestFullScreen();
		engine.requestPortrait();
		return engine;
	}

	@Override
	public E3Scene onLoadScene() {
		E3Scene scene = new E3Scene();
		scene.addEventListener(this);
		
		int centerX = (getWidth()  - robotTexture.getWidth())  / 2;
		int centerY = (getHeight() - robotTexture.getHeight()) / 2;
		
		robot = new Sprite(robotTexture, centerX, centerY) {
			@Override
			public void onRemove() {
				Debug.d("robot: onRemove()");
				super.onRemove();
			}
			@Override
			public void onDispose() {
				Debug.d("robot: onDispose()");
				super.onDispose();
			}
		};
		scene.getTopLayer().add(robot);
		
		Background background = new Background(
				new TiledTexture("background.png", getWidth(), getHeight(), this)) {
			@Override
			public void onRemove() {
				Debug.d("background: onRemove()");
				super.onRemove();
			}
			@Override
			public void onDispose() {
				Debug.d("background: onDispose()");
				super.onDispose();
			}
			@Override
			protected void unload(GL10 gl) {
				Debug.d("background: unload()");
				super.unload(gl);
			}
			
		};
		scene.getTopLayer().setBackground(background);
		
		Toast.makeText(this, "Touch screen to remove the sprite.", Toast.LENGTH_LONG).show();
		return scene;
	}

	@Override
	public void onLoadResources() {
		robotTexture = new AssetTexture("robot.png", this);
	}

	@Override
	public void onUnloadResources() {
		robot = null;
		robotTexture = null;
	}

	@Override
	public boolean onSceneTouchEvent(final E3Scene scene, MotionEvent motionEvent) {
		if (robot != null && motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
			scene.getTopLayer().remove(robot);
			// Sprite can't be used anymore after removal from layer.
			// If you just want to hide the sprite (that would be used later on),
			// use sprite's hide() or setVisible() method instead like below.
			// 
			// if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
			//	robot.setVisible(!robot.isVisible());
			//}
			//
			// The texture is also removed when Sprite is removed.
			// If you don't want the Texture removed, set Texture#setReusable(true).
		}
		return false;
	}
}
